﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

using HtmlMultiFileEditor.Controller;
using HtmlMultiFileEditor.Model;
namespace HtmlMultiFileEditor
{
    public partial class FormMainWindow : Form
    {

        #region "Attributes"

        private FileHandlerController fileHandlerController = FileHandlerController.Instance;
        private MessageHandlerController messageHandlerController = MessageHandlerController.Instance;
        private Manager manager = Manager.Instance;

        #endregion

        #region "Constructor"

        public FormMainWindow()
        {
            InitializeComponent();
            InitializeEventHandlers();
        }

        #endregion

        #region "Custom Events"

        public void InitializeEventHandlers()
        {
            fileHandlerController.FileListChanged += new FileHandlerController.FileListChangedEventHandler(fileHandlerController_FileListChanged);
            fileHandlerController.currentFileChanged += new FileHandlerController.CurrentFileChangeEventHandler(fileHandlerController_currentFileChanged);
            messageHandlerController.MessageChanged += new MessageHandlerController.MessageChangedHandler(messageHandlerController_MessageChanged);
            manager.FileModified += new Manager.ModificationEventHandler(manager_FileModified);
        }

        private void manager_FileModified(object sender, string eCurrentFile)
        {
            if (manager.Preview == Manager.PreviewMode.Enable)
            {
                webBrowserOutputPreview.Navigate("file:///" + eCurrentFile);
            }

            messageHandlerController.FileModifiedMessage(eCurrentFile);
        }

        private void messageHandlerController_MessageChanged(object sender, string eMessage)
        {
            currentStartCharacterIndex = 0;
            labelStatus.Text = eMessage;
        }

        private void fileHandlerController_currentFileChanged(object sender, string eCurrentFile)
        {
            labelCurrentFile.Text = eCurrentFile;

            if (manager.Preview == Manager.PreviewMode.Enable)
            {
                webBrowserInputPreview.Navigate("file:///" + eCurrentFile);

            }

            messageHandlerController.CurrentFileNameMessage(eCurrentFile);
        }

        private void fileHandlerController_FileListChanged(object sender, string[] e)
        {
            listBoxFile.Items.Clear();
            listBoxFile.Items.AddRange(e);
        }

        #endregion

        #region "UI Control Events"

        private void buttonExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {

            openFileDialog.Title = "Select file / files / folder";
            openFileDialog.Filter = "HTML Files (*.html)|*.html|HTM Files (*.htm)|*.htm ";
            if (openFileDialog.ShowDialog() == DialogResult.Cancel)
                return;

            try
            {

                if (openFileDialog.FileNames.Length > 1)
                {
                    string firstFilePath = openFileDialog.FileNames[0];
                    textBoxBrowse.Text = Path.GetDirectoryName(firstFilePath);

                    fileHandlerController.SetFilePath(textBoxBrowse.Text);
                    fileHandlerController.SetFileList(openFileDialog.FileNames);
                }
                else if (openFileDialog.FileNames.Length == 1)
                {

                    textBoxBrowse.Text = openFileDialog.FileName;
                    fileHandlerController.SetFilePath(openFileDialog.FileName);
                    fileHandlerController.SetFileList(openFileDialog.FileName);
                }

            }
            catch (Exception)
            { }

            messageHandlerController.FilesLoadedMessage();
        }

        private void textBoxBrowse_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (textBoxBrowse.Focused)
                {

                    fileHandlerController.SetFilePath(textBoxBrowse.Text);
                    fileHandlerController.SetFileList(textBoxBrowse.Text);

                }
            }
            catch (Exception)
            { }
        }

        private void listBoxFile_SelectedIndexChanged(object sender, EventArgs e)
        {
            string[] fileListSelected = new string[listBoxFile.SelectedItems.Count];
            listBoxFile.SelectedItems.CopyTo(fileListSelected, 0);
            fileHandlerController.SetFileListSelected(fileListSelected);

            fileHandlerController.SetFileListSelected(fileListSelected);


        }

        private void radioEnable_CheckedChanged(object sender, EventArgs e)
        {
            manager.Preview = Manager.PreviewMode.Enable;
        }

        private void radioDisable_CheckedChanged(object sender, EventArgs e)
        {
            manager.Preview = Manager.PreviewMode.Disable;
        }

        private void radioById_CheckedChanged(object sender, EventArgs e)
        {
            manager.Replacement = Manager.ReplacementMode.Id;
        }

        private void radioDirect_CheckedChanged(object sender, EventArgs e)
        {
            manager.Replacement = Manager.ReplacementMode.Direct;
        }

        private void radioButtonByClass_CheckedChanged(object sender, EventArgs e)
        {
            manager.Replacement = Manager.ReplacementMode.XPath;

        }

        private void radioButtonTag_CheckedChanged(object sender, EventArgs e)
        {
            manager.Replacement = Manager.ReplacementMode.Tag;
        }

        private void buttonNext_Click(object sender, EventArgs e)
        {
            fileHandlerController.NextFile();
        }

        private void buttonPrevious_Click(object sender, EventArgs e)
        {
            fileHandlerController.PreviousFile();
        }

        private void buttonCurrent_Click(object sender, EventArgs e)
        {
            Manager.Instance.Modify(Manager.ModifyMode.Current, Manager.Instance.Replacement, textBoxReplacement.Text, textBoxReplaceWith.Text);
        }

        private void buttonSelection_Click(object sender, EventArgs e)
        {
            Manager.Instance.Modify(Manager.ModifyMode.Selection, Manager.Instance.Replacement, textBoxReplacement.Text, textBoxReplaceWith.Text);
        }

        private void buttonAll_Click(object sender, EventArgs e)
        {
            Manager.Instance.Modify(Manager.ModifyMode.All, Manager.Instance.Replacement, textBoxReplacement.Text, textBoxReplaceWith.Text);
        }

        #endregion

        #region "Status Message Animation"

        private int currentStartCharacterIndex = 0;
        private void timerMessage_Tick(object sender, EventArgs e)
        {

            if (currentStartCharacterIndex >= manager.Message.Length)
            {
                currentStartCharacterIndex = 0;
                labelStatus.Text = manager.Message;
            }
            else if (currentStartCharacterIndex <= manager.Message.Length)
            {
                currentStartCharacterIndex++;
                labelStatus.Text = manager.Message.Substring(currentStartCharacterIndex);
            }
        }

        private void labelStatus_Click(object sender, EventArgs e)
        {
            currentStartCharacterIndex = 0;

            if (timerMessage.Enabled)
                timerMessage.Enabled = false;
            else
                timerMessage.Enabled = true;
        }

        #endregion


    }
}
